package com.maple.exclusivesport.adapters;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.maple.exclusivesport.ConfirmDialog;
import com.maple.exclusivesport.ContactInfoBean;
import com.maple.exclusivesport.DataBaseOpenHelper;
import com.maple.exclusivesport.GlideLoadUtils;
import com.maple.exclusivesport.PersonImageDialog;
import com.maple.exclusivesport.R;
import com.maple.exclusivesport.SendEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.List;

/**
 * Created by maple on 2019/10/17 10:17
 * E-Mail Address：740917401@qq.com
 */
public class MyAdapter extends RecyclerView.Adapter
        <ContactViewHolder> { //MyAdapter类 开始

    //MyAdapter的成员变量contactInfoList, 这里被我们用作数据的来源
    private List<ContactInfo> contactInfoList;

    Context context;
    //MyAdapter的构造器
    public MyAdapter(Context context, List<ContactInfo> contactInfoList) {
        this.context=context;
        this.contactInfoList = contactInfoList;
    }
    //重写3个抽象方法
//onCreateViewHolder()方法 返回我们自定义的 ContactViewHolder对象
    @Override
    public ContactViewHolder onCreateViewHolder
    (ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.card_view,parent,false);
        return new ContactViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder
            (ContactViewHolder holder, int position) {

//contactInfoList中包含的都是ContactInfo类的对象
//通过其get()方法可以获得其中的对象
        ContactInfo ci = contactInfoList.get(position);

//将viewholder中hold住的各个view与数据源进行绑定(bind)
        holder.date.setText(ci.date);
        holder.weight.setText("Weight: "+ci.weight+" kg");
        holder.content.setText("Journal: "+ci.content);


        GlideLoadUtils.getInstance().glideLoad(context,Uri.fromFile(new File(ci.image)),holder.image,-1);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PersonImageDialog(context,ci.image).show();
            }
        });
        holder.delete.setOnClickListener((v)->
            new ConfirmDialog(context, "提示", "请确认是否删除该条掉肉圈？", new ConfirmDialog.onConfirmDialogListener() {
                @Override
                public void onConfirm() {
                    contactInfoList.remove(ci);
                    DataBaseOpenHelper.getInstance(context).deleteContactInfo(ci);
                    EventBus.getDefault().post(new SendEvent());
                }

                @Override
                public void onCancel() {

                }
            }).show()
        );

    }

    //此方法返回列表项的数目
    @Override
    public int getItemCount() {
        return contactInfoList.size();
    }


} //结束 类MyAdapter
